﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Linq;
namespace Exact_Extension
{
    public partial class DiscountAttach : DevExpress.XtraEditors.XtraForm
    {
        ISHCMC_V_DiscountList RootDis;
        ExactDataContext DS;
        Common common;
        public DiscountAttach(ExactDataContext _DS ,ISHCMC_V_DiscountList Dis)
        {
            InitializeComponent();
            RootDis = Dis;
            DS = _DS;
            common = new Common(DS);
            this.LoadListAttachment();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.ShowDialog();
            this.txtFilePath.Text = openFileDialog1.FileName;
        }
        private void LoadListAttachment()
        {
            var queryAttach = from DisAttach in DS.ISHCMC_DiscountAttachments
                              where DisAttach.StudentID == RootDis.StudentID 
                              && DisAttach.DiscountID == RootDis.ID
                              select DisAttach;

            this.iSHCMCDiscountAttachmentBindingSource.DataSource = queryAttach;
            if (queryAttach.Count() > 0)
            {
                this.gridView_Attachment.SelectRow(0);
            }
        }
        private void btnAttach_Click(object sender, EventArgs e)
        {
            try
            {
                FileInfo fileAttach = new FileInfo(this.txtFilePath.Text);
                FileStream fs;

                fs = fileAttach.OpenRead();

                //a byte array to read the image

                byte[] picbyte = new byte[fs.Length];

                fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));

                fs.Close();

                ISHCMC_DiscountAttachment DisAttach = new ISHCMC_DiscountAttachment()
                {
                    DiscountID = RootDis.ID,
                    DiscountType = RootDis.DiscountType,
                    StudentID = RootDis.StudentID,
                    FileTitle = this.txtTitle.Text,
                    Remarks = this.txtRemark.Text,
                    FileExtension = fileAttach.Extension,
                    AttachDate = DateTime.Now.Date,
                    AttachmentFile = picbyte,
                    Syscreated = DateTime.Now,
                    Sysmodified = DateTime.Now,
                    Syscreator = Environment.UserName,
                    Sysmodifier = Environment.UserName
                };
                DS.ISHCMC_DiscountAttachments.InsertOnSubmit(DisAttach);
                DS.SubmitChanges();
               
                this.LoadListAttachment();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridView_Attachment_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            ISHCMC_DiscountAttachment InvAttach = (ISHCMC_DiscountAttachment)this.gridView_Attachment.GetRow(e.FocusedRowHandle);

            MemoryStream ms = new MemoryStream();
 
                ms.Write(InvAttach.AttachmentFile.ToArray(), 0, InvAttach.AttachmentFile.Length);



                this.picture_Attachment.ShowToolTips = true;
                this.picture_Attachment.ToolTipTitle = InvAttach.FileTitle;
                this.picture_Attachment.ToolTip = InvAttach.Remarks; 
                this.picture_Attachment.Image = Image.FromStream(ms);
                this.picture_Attachment.Refresh();
        }
    }
}